import { getCookie } from "@/secondary/utils/cache";
import { NavigationGuardNext, RouteLocationNormalized } from "vue-router";

// 登录状态失效时，跳转登录页面
export const checkLoginStatus = (to: RouteLocationNormalized, next: NavigationGuardNext) => {
  const token = getCookie("token");
  const { path } = to;
  const isLoginPage = ["/login"].includes(path);
  const isLogoutPage = ["/logout"].includes(path);

  if (!token && !isLoginPage && !isLogoutPage) {
    next({
      path: "/logout",
      replace: true,
    });
    return Promise.reject("当前登录状态失效，请重新登录");
  }

  return Promise.resolve(true);
};
